//	Audio classes
//	The audio is split into several classes
//	Each class is instanciated once during
//	the project's setup.
//	Some classes will allocate audio channels
//	in order to avoid unwanted overlap on critical sfx.
//	However, it is unsure that the result will be 
//	more reliable than a fully automatic audio handler
//	(ie. no allocated channels)

		Include("data/scripts/globals.nut")

//	Streams OGG music(s)
//	In the future, this class may be able to fade in/out
//	or even cross fade between 2 tracks.
class	Music
{
	mixer				=	0
	music_channel		=	-1

	constructor()
	{
		mixer	=	g_mixer
	}

	function	PlayMusicTrack(track_number)
	{
		if (!g_sound_enable)
			return

		if (music_channel == -1)
			music_channel = MixerChannelLock(mixer)

		MixerChannelStartStream(mixer, music_channel, GetMusicTrackFilename(track_number))
		MixerChannelSetGain(mixer, music_channel, 0.5)
		MixerChannelSetLoopMode(mixer, music_channel, LoopRepeat)
	}

	function	GetMusicTrackFilename(track_number)
	{	return ("data/sfx/musics/track_" + track_number.tostring() + ".ogg") }
}

class	AudioBase
{
	mixer		=	0
	tracks		=	0
		
	constructor()
	{
		mixer	=	g_mixer
		tracks	=	[]
	}
	
	function	AllocateChannels(n)
	{
		local	i
		for(i = 0; i < n; i++)
			tracks.append(MixerChannelLock(mixer))
	}

	function	DeallocateChannels()
	{
		local	c
		foreach (c in tracks)
			MixerChannelUnlock(mixer, c)
	}
}

class	EnvironmentAudio extends AudioBase
{
	sfx_door_open			=	0

	constructor()
	{
		print("EnvironmentAudio::constructor()")
		base.constructor()
		sfx_door_open		=	[]
		sfx_door_open.append(LoadGameSfx("sfx_door_open.wav"))
	}

	function	PlaySfxDoorOpen()
	{	MixerSoundStart(mixer, sfx_door_open[0])	}
}

class	MobileAudio	extends	AudioBase
{

	sfx_shoot				=	0
	sfx_hit					=	0
	sfx_explode				=	0
	root_dir				=	""
	shoot_channel_switch	=	0

	constructor(_root_dir)
	{
		print("MobileAudio::constructor()")
		base.constructor()
		base.AllocateChannels(2)

		root_dir = _root_dir
		
		sfx_shoot	=	[]
		for(local	i = 0; i < 3; i++)
			sfx_shoot.append(LoadSfxFile("shoot_" + i.tostring() + ".wav"))

		sfx_hit		=	[]
		for(local	i = 0; i < 3; i++)
			sfx_hit.append(LoadSfxFile("hit_" + i.tostring() + ".wav"))

		sfx_explode = []
		sfx_explode.append(LoadSfxFile("explode.wav"))
	}

	function	LoadSfxFile(fname)
	{
		return LoadGameSfx(root_dir + "/" + fname)
	}

	function	PlaySfxShoot()
	{
		if (!g_sound_enable)
			return

		local	sfx_rand = Mod(Irand(0,100), sfx_shoot.len())
		MixerChannelStart(mixer, tracks[shoot_channel_switch], sfx_shoot[sfx_rand])
		shoot_channel_switch = 1 - shoot_channel_switch			
	}

	function	PlaySfxHit()
	{
		if (!g_sound_enable)
			return

		local	sfx_rand = Mod(Irand(0,100), sfx_hit.len())
		MixerSoundStart(mixer, sfx_hit[sfx_rand])				
	}

	function	PlaySfxExplode()
	{
		if (g_sound_enable)
			MixerSoundStart(mixer, sfx_explode[0])
	}	
}

class	TankAudio extends MobileAudio
{
	
	sfx_spawn		=	0
	sfx_powerup		=	0
	sfx_got_key		=	0
	sfx_use_key		=	0

	constructor()
	{
		print("TankAudio::constructor()")
		base.constructor("tank")
		sfx_spawn	=	[]
		sfx_spawn.append(base.LoadSfxFile("spawn.wav"))
		sfx_powerup	=	[]
		sfx_powerup.append(base.LoadSfxFile("powerup.wav"))
		sfx_got_key	=	[]
		sfx_got_key.append(base.LoadSfxFile("got_key.wav"))
		sfx_use_key	=	[]
		sfx_use_key.append(base.LoadSfxFile("use_key.wav"))
	}

	function	PlaySfxSpawn()
	{	if(g_sound_enable)	MixerSoundStart(mixer, sfx_spawn[0])	}

	function	PlaySfxPowerUp()
	{	if(g_sound_enable)	MixerSoundStart(mixer, sfx_powerup[0])	}

	function	PlaySfxShoot()
	{	if(g_sound_enable)	base.PlaySfxShoot()	}

	function	PlaySfxHit()
	{	if(g_sound_enable)	base.PlaySfxHit()	}

	function	PlaySfxExplode()
	{	if(g_sound_enable)	base.PlaySfxExplode()	}

	function	PlaySfxGotKey()
	{	if(g_sound_enable)	MixerSoundStart(mixer, sfx_got_key[0])	}

	function	PlaySfxUseKey()
	{	if(g_sound_enable)	MixerSoundStart(mixer, sfx_use_key[0])	}
}
